package com.richapp.launcher.ui.activities;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.richapp.launcher.R;

import com.richapp.launcher.net.mobile.server.UserServer;
import com.richapp.launcher.serialport.gateway.command.AddCmd;
import com.richapp.launcher.serialport.infrared.InfraredServer;
import com.richapp.launcher.serialport.infrared.message.ControlMessage;
import com.richapp.launcher.serialport.infrared.message.StudyMessage;
import com.richapp.launcher.ui.fragments.home.Tabs_Fragment;
import com.richapp.launcher.utils.ServiceUtil;
import com.richapp.launcher.utils.Tools;
import com.richapp.net.util.HexTool;
import com.tencent.android.tpush.XGPushManager;

public class MainFragmentActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		//设置无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//设置全屏显示
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

		setContentView(R.layout.main);
		initView();

        //导入数据
        Tools.importDatabaseRawFile(this);

        //启动服务
        ServiceUtil.startService(this);

        //更新UI信息
        UserServer.getInstance().setContext(this);
        UserServer.getInstance().userInfoUpdate();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		String msg = "未定义的按键!";
		if(getResources().getInteger(R.integer.wps_short_value) == event.getKeyCode()){
			msg = "短按|WPS键!";
			Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();

			AddCmd.getInstance().blindAdd();

		}else if(getResources().getInteger(R.integer.wps_long_value) == event.getKeyCode()){
            msg = "长按|WPS键!";
			Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	private void initView() {
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		localFragmentTransaction.replace(R.id.tabs_fragment,new Tabs_Fragment());
		localFragmentTransaction.commit();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

    @Override
    protected void onResume() {
        super.onResume();
        XGPushManager.onActivityStarted(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        XGPushManager.onActivityStoped(this);
    }

    private void testInfraredControl(){
        ControlMessage controlMessage = new ControlMessage();
        controlMessage.setDeviceId((byte) 0x01);
        //Remote_obj remote_obj = new Remote_obj();

        //byte[] controlData = remote_obj.Encode_str("449A6540646464FC5E100000FF45BA").getBytes();
        byte[] controlData = HexTool.HexToByteArr("03601800181830189D0B019200");
        controlMessage.setControlData(controlData);
        int length = controlData.length + 2;
        String lengthStr = null;
        if(length > 255){
            lengthStr = Integer.toHexString(length);
        }else{
            lengthStr = "00" + Integer.toHexString(length);
        }

        controlMessage.setLength(HexTool.HexToByteArr(lengthStr));
        InfraredServer.getInstance().send(controlMessage);
    }

    private void testInfraredSendStudy(){
        ControlMessage controlMessage = new ControlMessage();
        controlMessage.setDeviceType((byte) 0x30);
        controlMessage.setDeviceId((byte)0x01);
        byte[] controlData = HexTool.HexToByteArr("0177822B8116150E13391311133913111339133913111339133913121211133913111339131113391339133913391339160E133913111311131112111311131113111311131113111311131113111311133913111311131113111311131113111311133913111311131113111311131113391311131113111311131113111311131112111311131113121239131113391339150F131113972E8241812414131513153B153B1513141314131513143B153B153B15131513141314131513141314131413153B15131413141314131413141315131413143B151314131413141315131413141314131513141314131413141314131513143B15131513141314131513141314131413141314131413143B15131513141314131513141314131413141314131413143B1514141314131513141314131413143B153B15131413143B153B1513141414971C793F100F0F110F2B0E2C0F110F120E2B0E120E120E120E2C0F110F2B0E2C0F110E110F110F2B0E110E2C0F110F2B0E2C0F");
        controlMessage.setControlData(controlData);
        int length = controlData.length + 2;
        String lengthStr;
        if(length > 255){
            lengthStr = Integer.toHexString(length);
        }else{
            lengthStr = "00" + Integer.toHexString(length);
        }

        controlMessage.setLength(HexTool.HexToByteArr(lengthStr));
        InfraredServer.getInstance().send(controlMessage);
    }

    private void testInfraredStudy(){
        StudyMessage studyMessage = new StudyMessage();
        studyMessage.setButtonId((byte) 108);
        InfraredServer.getInstance().send(studyMessage);
    }


}
